%clear all; clc;
close all;
%Updated: 12/1/2021

%Compute impulse-response functions using finer endowment and debt grids
% #endowment gridpoints (na2)=501,   #debt gridpoints (nb2)=3501
% Initial endowment level: uncoditional mean
% Feed in one-std-deviation shock in period 1

lambda = 0.184;
rf     = 0.02;

%case 1
cd 'irfmc_0.75bss'           %initial debt: 0.75% of s.s. debt

p   =load('irfp.txt');
h   =load('irfh.txt');
b   =load('irfb.txt');
ct  =load('irfct.txt');
r   =load('irfr.txt');
gn  =load('irfgn.txt');
tau =load('irftau.txt');
a   =load('irfa.txt');
cn  =load('irfcn.txt');
ww  =load('irfww.txt');
bor =load('irfbor.txt');

p0   =p(:,1);
h0   =h(:,1);
b0   =b(:,1);
ct0  =ct(:,1);
r0   =r(:,1);
gn0  =gn(:,1);
tau0 =tau(:,1);
a0   =a(:,1);
cn0  =cn(:,1);
ww0  =ww(:,1);
bor0 =bor(:,1);

%case 2
cd ..
cd 'irfmc_bss'                %initial debt: s.s. debt 

p   =load('irfp.txt');
h   =load('irfh.txt');
b   =load('irfb.txt');
ct  =load('irfct.txt');
r   =load('irfr.txt');
gn  =load('irfgn.txt');
tau =load('irftau.txt');
a   =load('irfa.txt');
cn  =load('irfcn.txt');
ww  =load('irfww.txt');
bor =load('irfbor.txt');

p15   =p(:,1);
h15   =h(:,1);
b15   =b(:,1);
ct15  =ct(:,1);
r15   =r(:,1);
gn15  =gn(:,1);
tau15 =tau(:,1);
a15   =a(:,1);
cn15  =cn(:,1);
ww15  =ww(:,1);
bor15 =bor(:,1);

cd  ..

b   = -b;   
b0  = -b0; 
b15 = -b15; 

nirf = 20;      %number of periods
time = 1:nirf;

NameArray = {'LineStyle','Color','LineWidth'};
ValueArray = {'--','-','-.';[0.23137255012989 0.443137258291245 0.337254911661148],[0.078  0.1686  0.549],[1  0.4  0.4];1.25,1.25,1.25}';

endt=15;

NameArray = {'LineStyle','Color','LineWidth'};
ValueArray1 = {'--';[0.078  0.1686  0.549];2}';
ValueArray2 = {'--','-';[0.078  0.1686  0.549],[0.8  0  0];2,2}';  %[1  0.4  0.4]

f = warndlg(['Blue: y^T=uncond mean, b=0.75*(steady state debt) ';...
             'Red:  y^T=uncond mean, b=steady state             '],'Initial state');
    
height=4;
width=5;

nameFig = char(['yt0' '_' char(label_graph)]);
figure('name',nameFig);
P=plot(time,a(1:nirf,1));grid on;
AX1 = gca;box on;
set(AX1,'YTick',-0.04:0.02:0,'XTick',[1,5,10,15],'FontSize',16);
axis(AX1, [1 time(endt) -0.04 0]);
set(P,NameArray,ValueArray1);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
exportfig(gcf, fullfile(plot_path, nameFig),'color','cmyk','resolution',1600);
nameFig = char(['h0' '_' char(label_graph)]);
figure('name',nameFig);
P=plot(time,-100*h0(1:nirf,1));grid on;ylabel('%','FontSize',16);legend('low debt','Location','NorthEast','FontSize',16,'Interpreter','latex'); 
legend boxoff; 
set(P,NameArray,ValueArray1);
AX1 = gca;box on;
set(AX1,'YTick',0:5:10,'XTick',[1,5,10,15],'FontSize',16);
axis(AX1, [1 time(endt) -0.4 10]);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
exportfig(gcf, fullfile(plot_path, nameFig),'color','cmyk','resolution',1600);
nameFig = char(['p0' '_' char(label_graph)]);
figure('name',nameFig);
P=plot(time,p0(1:nirf,1));grid on;
AX1 = gca;box on;
set(AX1,'YTick',-0.1:0.05:0,'XTick',[1,5,10,15],'FontSize',16);
axis(AX1, [1 time(endt) -0.1 0.01]);
set(P,NameArray,ValueArray1);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
exportfig(gcf, fullfile(plot_path, nameFig),'color','cmyk','resolution',1600);
nameFig = char(['gn0' '_' char(label_graph)]);
figure('name',nameFig);
P=plot(time,gn0(1:nirf,1));grid on;
AX1 = gca;box on;
set(AX1,'YTick',-0.03:0.01:0.01,'XTick',[1,5,10,15],'FontSize',16);
axis(AX1, [1 time(endt) -0.03 0.01]);
set(P,NameArray,ValueArray1);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
exportfig(gcf, fullfile(plot_path, nameFig),'color','cmyk','resolution',1600);
nameFig = char(['b0' '_' char(label_graph)]);
figure('name',nameFig);
P=plot(time,b0(1:nirf,1));grid on;
set(P,NameArray,ValueArray1);
AX1 = gca;box on;
set(AX1,'YTick',-0.06:0.06:0.06,'XTick',[1,5,10,15],'FontSize',16);
axis(AX1, [1 time(endt) -0.06 0.06]);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
exportfig(gcf, fullfile(plot_path, nameFig),'color','cmyk','resolution',1600);
nameFig = char(['r0' '_' char(label_graph)]);
figure('name',nameFig);
P=plot(time,100*r0(1:nirf,1));grid on;ylabel('%','FontSize',16);
set(P,NameArray,ValueArray1);
AX1 = gca;box on;
set(AX1,'YTick',0.0:0.5:1,'XTick',[1,5,10,15],'FontSize',16);
axis(AX1, [1 time(endt) -0.05 1]);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
exportfig(gcf, fullfile(plot_path, nameFig),'color','cmyk','resolution',1600);
nameFig = char(['ct0' '_' char(label_graph)]);
figure('name',nameFig);
P=plot(time,ct0(1:nirf,1));grid on;
set(P,NameArray,ValueArray1);
AX1 = gca;box on;
set(AX1,'YTick',-0.08:0.04:0.0,'XTick',[1,5,10,15],'FontSize',16);
axis(AX1, [1 time(endt) -0.08 0.004]);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
exportfig(gcf, fullfile(plot_path, nameFig),'color','cmyk','resolution',1600);
nameFig = char(['cn0' '_' char(label_graph)]);
figure('name',nameFig);
P=plot(time,cn0(1:nirf,1));grid on;
set(P,NameArray,ValueArray1);
AX1 = gca;box on;
set(AX1,'YTick',-0.063:0.02:0.003,'XTick',[1,5,10,15],'FontSize',16);
axis(AX1, [1 time(endt) -0.06 0.002]);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
exportfig(gcf, fullfile(plot_path, nameFig),'color','cmyk','resolution',1600);

nameFig = char(['yt1' '_' char(label_graph)]);
figure('name',nameFig);
P=plot(time,a(1:nirf,1),time,a(1:nirf,1));grid on;
AX1 = gca;box on;
set(AX1,'YTick',-0.04:0.02:0,'XTick',[1,5,10,15],'FontSize',16);
axis(AX1, [1 time(endt) -0.043 0.003]);
set(P,NameArray,ValueArray2);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
saveas(gcf, fullfile(plot_path, nameFig),format_chart);
nameFig = char(['h1' '_' char(label_graph)]);
figure('name',nameFig);
P=plot(time,-100*h0(1:nirf,1),time,-100*h15(1:nirf,1));grid on;ylabel('$\%$','FontSize',16,'Interpreter','latex');legend('low debt','high debt','Location','NorthEast','FontSize',16,'Interpreter','latex');%title('unemployment','Fontweight','normal');
legend boxoff;
AX1 = gca;box on;
set(AX1,'YTick',0:5:10,'XTick',[1,5,10,15],'FontSize',16);
axis(AX1, [1 time(endt) -0.65 10.65]);
set(P,NameArray,ValueArray2);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
saveas(gcf, fullfile(plot_path, nameFig),format_chart);
nameFig = char(['p1' '_' char(label_graph)]);
figure('name',nameFig);
P=plot(time,p0(1:nirf,1),time,p15(1:nirf,1));grid on;
AX1 = gca;box on;
set(AX1,'YTick',-0.1:0.05:0,'XTick',[1,5,10,15],'FontSize',16);
axis(AX1, [1 time(endt) -0.11 0.01]);
set(P,NameArray,ValueArray2);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
saveas(gcf, fullfile(plot_path, nameFig),format_chart);
nameFig = char(['gn1' '_' char(label_graph)]);
figure('name',nameFig);
P=plot(time,gn0(1:nirf,1),time,gn15(1:nirf,1));grid on;
AX1 = gca;box on;
set(AX1,'YTick',-0.03:0.01:0.01,'XTick',[1,5,10,15],'FontSize',16);
axis(AX1, [1 time(endt) -0.033 0.013]);
set(P,NameArray,ValueArray2);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
saveas(gcf, fullfile(plot_path, nameFig),format_chart);
nameFig = char(['b1' '_' char(label_graph)]);
figure('name',nameFig);
P=plot(time,b0(1:nirf,1),time,b15(1:nirf,1));grid on;
set(P,NameArray,ValueArray2);
AX1 = gca;box on;
set(AX1,'YTick',-0.06:0.06:0.06,'XTick',[1,5,10,15],'FontSize',16);
axis(AX1, [1 time(endt) -0.07 0.07]);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
saveas(gcf, fullfile(plot_path, nameFig),format_chart);
nameFig = char(['r1' '_' char(label_graph)]);
figure('name',nameFig);
P=plot(time,100*r0(1:nirf,1),time,100*r15(1:nirf,1));grid on;ylabel('$\%$','FontSize',16,'Interpreter','latex');%title('spreads','Fontweight','normal');
set(P,NameArray,ValueArray2);
AX1 = gca;box on;
set(AX1,'YTick',0.0:0.5:1,'XTick',[1,5,10,15],'FontSize',16);
axis(AX1, [1 time(endt) -0.1 1.1]);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
saveas(gcf, fullfile(plot_path, nameFig),format_chart);
nameFig = char(['ct1' '_' char(label_graph)]);
figure('name',nameFig);
P=plot(time,ct0(1:nirf,1),time,ct15(1:nirf,1));grid on;
set(P,NameArray,ValueArray2);
AX1 = gca;box on;
set(AX1,'YTick',-0.08:0.04:0.0,'XTick',[1,5,10,15],'FontSize',16);
axis(AX1, [1 time(endt) -0.085 0.005]);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
saveas(gcf, fullfile(plot_path, nameFig),format_chart);

nameFig = char(['cn1' '_' char(label_graph)]);
P=plot(time,cn0(1:nirf,1),time,cn15(1:nirf,1));grid on;
set(P,NameArray,ValueArray2);
AX1 = gca;box on;
set(AX1,'YTick',-0.06:0.02:0,'XTick',[1,5,10,15],'FontSize',16);
axis(AX1, [1 time(endt) -0.065 0.005]);
set(gcf, 'Units', 'Inches', 'Position', [2, 2, width, height]); %[initial x position, initial y position, width, height]
saveas(gcf, fullfile(plot_path, nameFig),format_chart);

